home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / btoc_v12.lha / BtoCDir / ExplodeStuff / SAS_C / Explode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-27  |  2.7 KB  |  112 lines

  1. #define PROGRAM_NAME    "Explode"
  2. #define VERSION        "v3.0"
  3.  
  4. /* Explode is to be used together with Decrunch30.s */
  5. /* The  Makefile  included  will compile both these */
  6. /* files to create a working program.               */
  7.  
  8. /* This version of Explode uses Decrunch30.S to explode  */
  9. /* data compressed with BtoC v2.5 or upper. Use it as an example. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <libraries/dos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20.  
  21. #define BTOC_MSG    ( (ULONG)'B'<<24L | (ULONG)'T'<<16L | 'O'<<8 | 'C' )
  22.  
  23. char *NO_MEM = "No memory.";
  24. char *USAGE = "\x1B[33m"PROGRAM_NAME"\x1B[0m "VERSION
  25. " - to test the compression algorytmh of BtoC "VERSION
  26. "\nBy \x1B[3m\x1B[2mStefano Reksten \x1B[0mof 3AM - \x1B[1mThe Three Amigos!!!\x1B[0m\n"
  27. "Usage: "PROGRAM_NAME" <file.CMPR>";
  28.  
  29.  
  30. ULONG data_size;
  31. BPTR handle;
  32. UBYTE *expl_ptr, *cmpr_ptr;
  33. UBYTE most_used;
  34. BOOL verbose=FALSE;
  35. char filename[128];
  36.  
  37. extern BOOL __asm Decrunch30( register __a0 UBYTE *, register __a1 UBYTE * );
  38.  
  39.  
  40. void Fail(char*);
  41. int main(int,char**);
  42.  
  43.  
  44. void Fail( char *s )
  45. {
  46. if ( s )    puts( s );
  47. if ( handle )    Close( handle );
  48. if ( expl_ptr )    FreeMem( expl_ptr, data_size );
  49. if ( cmpr_ptr )    FreeMem( cmpr_ptr, data_size );
  50.  
  51. exit( 0 );
  52. }
  53.  
  54.  
  55. int main( int argc, char **argv )
  56. {
  57. if ( argc==1 )
  58.     Fail( USAGE );
  59.  
  60. puts( PROGRAM_NAME );
  61.  
  62. strcpy( filename, argv[1+verbose] );
  63. printf( "File choosen : %s\n", filename );
  64.  
  65. if ( !( handle=Open( filename, MODE_OLDFILE ) ) )
  66.     Fail( "No file" );
  67.  
  68. Read( handle, &data_size, sizeof(ULONG) );
  69. if ( data_size != BTOC_MSG )
  70.     Fail( "Data NOT compressed with BtoC" );
  71.  
  72. Read( handle, &data_size, sizeof(ULONG) );
  73. Read( handle, &most_used, sizeof(UBYTE) );
  74.  
  75. if ( !(expl_ptr=(UBYTE *)AllocMem( data_size, MEMF_PUBLIC ) ) )
  76.     Fail( NO_MEM );
  77.  
  78. if ( !(cmpr_ptr=(UBYTE *)AllocMem( data_size, MEMF_PUBLIC ) ) )
  79.     Fail( NO_MEM );
  80.  
  81. /* Actually, you don't need to Alloc "data_size" bytes, as you    */
  82. /* compressed your data. You should know the exact size of the    */
  83. /* compressed file. (Lock, Examine & Alloc, or WRITE IT ON A    */
  84. /* PIECE OF PAPER!!!)                        */
  85. /* Doing THIS way is a waste of memory.                */
  86.  
  87.  
  88. Seek( handle, 0, OFFSET_BEGINNING );
  89. Read( handle, cmpr_ptr, data_size );
  90. Close( handle ); handle = NULL;
  91.  
  92. printf( "Now decrunching.\n" );
  93.  
  94. if ( Decrunch30( expl_ptr, cmpr_ptr ) )
  95.     {
  96.     printf( "Done, writing .EXPL file.\n" );
  97.  
  98.     if ( !stricmp( filename+strlen( filename )-4, ".CBM" ) )
  99.         filename[ strlen( filename )-4 ] = NULL;
  100.  
  101.     strcat( filename, ".EXPL" );
  102.  
  103.     if ( !(handle = Open( filename, MODE_NEWFILE ) ) )
  104.         Fail( "File not opened" );
  105.  
  106.     Write( handle, expl_ptr, data_size );
  107.  
  108.     Fail( "File exploded." );
  109.     }
  110. else    Fail( "Did *NOT* decrunch!" );
  111. }
  112.